package com.dayu.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.dayu.entity.Msuser;
import com.dayu.service.MsuserService;

/**
 * @author dayu
 * @date 2018/11/01
 */
@Controller
@RequestMapping("msuser")
public class MsuserController {

    @Autowired
    private MsuserService msuserService;

    @RequestMapping("msuserList")
    public String msuserList(Model model) {
        List<Msuser> msuserList = msuserService.getMsuserList();
        model.addAttribute("msuserList", msuserList);
        for (Msuser msuser : msuserList) {
            System.out.println(msuser);
        }
        return "msuser/userList";
    }

    @RequestMapping("gotoAdd")
    public String gotoAdd() {
        return "msuser/gotoAdd";
    }

    @RequestMapping(value="add",method=RequestMethod.POST)
    public String add(Msuser msuser, Model model) {
        System.out.println(msuser);
        msuserService.insertMsuser(msuser);
//        List<Msuser> msuserList = msuserService.getMsuserList();
//        model.addAttribute("msuserList", msuserList);
        return "redirect:msuserList";
    }

    @RequestMapping("gotoUpdateMsuser")
    public String gotoUpdateMsuser(int id, Model model) {
        Msuser msuser = msuserService.getMsuserById(id);
        model.addAttribute("msuser", msuser);
        return "msuser/updateUser";
    }

    @RequestMapping(value="updateMsuser",method=RequestMethod.POST)
    public String updateMsuser(Msuser msuser, Model model) {
        msuserService.updateMsuser(msuser);
        return "redirect:msuserList";
    }

    @RequestMapping("deleteMsuser")
    public String deleteMsuser(int id, Model model) {
        msuserService.deleteMsuserById(id);
        return "redirect:msuserList";
    }

}
